package com.launcher.wkang.view;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

import com.launcher.wkang.activity.LauncherActivity;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import com.launcher.wkang.R;

public class ClockSurfaceView extends SurfaceView  implements Callback {
    private float minuteDegree;
    private float hourDegree;
    private Calendar mCalendar;
    private int mHour;
    private int mMinuts;
    private Paint mPaint;
    private Context context;
    private SurfaceHolder surfaceHolder;
    private  Canvas canvas = null;

    public ClockSurfaceView(Context context) {
        super(context);
         this.context=context;
        init(); //初始化,设置生命周期回调方法

    }
    public ClockSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context=context;
        init(); //初始化,设置生命周期回调方法
    }

    public ClockSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context=context;
        init(); //初始化,设置生命周期回调方法
    }

    private void init(){

        SurfaceHolder holder = getHolder();
        holder.addCallback(this); //设置Surface生命周期回调
        mPaint = new Paint();
        //抗锯齿
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.BLACK);
        //分三种，STROKE之绘制轮廓，不绘制内容；FILL，只绘制内容；FILL_AND_STROKE，内容和轮廓都绘制
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
                               int height) {
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        surfaceHolder=holder;
        timer.schedule(timerTask, 0, 1000*60);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
    }

    private Timer timer = new Timer();
    private TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {

            mCalendar=Calendar.getInstance();
            mHour=mCalendar.get(Calendar.HOUR);
            mMinuts=mCalendar.get(Calendar.MINUTE);
//            if (minuteDegree == 360) {
//                minuteDegree = 0;
//            }
//            if (hourDegree == 360) {
//                hourDegree = 0;
//            }

            //这三个变量的换算方式，变量名起分针和秒针起反了，也无所谓了
            //第一个360/60=6，也就是一秒钟走六度
            //第二个6/60 分针一秒针走0.1度
            //时针，一秒钟走1/120度
            hourDegree = (float) ((mHour+(mMinuts/60.0))*30);
            minuteDegree = mMinuts*6;

            /**
             * 自定义View 刷新界面有三种
             * 1：Invalidate() 如果只是内容变动，可使用此方法
             * 2：postInvalidate() 涉及到线程切换的
             * 3：requestLayout() view位置变动，需要调用此方法 涉及到RadioGroup
             */
            postInvalidate();//涉及到线程，界面刷新需要使用此方法

//            c = surfaceHolder.lockCanvas(null);
                synchronized (surfaceHolder) {
                    if (canvas==null) {
                        canvas = surfaceHolder.lockCanvas(null);
                    }
                    doDraw(canvas);
                    //通过它来控制帧数执行一次绘制后休息50ms
                }
        }
    };

    protected void doDraw(Canvas canvas) {

        canvas.translate(getWidth() / 2, getHeight() / 2);
        //分针
        canvas.save();
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.STROKE);//绘制边框
        mPaint.setStrokeWidth(4);//边框宽度  比指针粗点
        Matrix minuteMatrix=new Matrix();
        minuteMatrix.postTranslate(-(int)(BitmapFactory.decodeResource(getResources(),R.mipmap.minute ).getWidth()*0.5),-(int)(BitmapFactory.decodeResource(getResources(),R.mipmap.minute ).getHeight()*0.86));
        minuteMatrix.postRotate(minuteDegree,0,0);
        canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.minute ),minuteMatrix,mPaint);
        canvas.restore();
        //时针
        canvas.save();
        mPaint.setStyle(Paint.Style.STROKE);//绘制边框
        mPaint.setStrokeWidth(10);//边框宽度  比指分针粗点
        Matrix hourMatrix=new Matrix();
        hourMatrix.postTranslate(-(int)(BitmapFactory.decodeResource(getResources(),R.mipmap.hour ).getWidth()*0.5),-(int)(BitmapFactory.decodeResource(getResources(),R.mipmap.hour ).getHeight()*0.85));
        hourMatrix.postRotate(hourDegree,0,0);
//        hourMatrix.setRotate(hourDegree+360);
        canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.hour ),hourMatrix,mPaint);
        canvas.restore();
    }


}
